package com.cs157a.view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import com.cs157a.controller.AposApplicationController;
import com.cs157a.dao.RdiPart;

/**
 * This class displays table summary of parts in the top right panel of the main
 * view.
 * 
 * @author hhong
 * 
 */
public class PartSummaryPanel extends JPanel {
	private static final long serialVersionUID = 20012100301L;

	private JTable table;
	private PartTableModel tableModel;
	private JButton orderButton;
	private JButton deleteButton;
	private JLabel partLabel;
	private JTextField partCountField;
	private JPanel countPanel;
	private List<RdiPart> partList;

	public PartSummaryPanel(final AposApplicationController controller,
			final List<RdiPart> parts) {
		controller.setPartSummaryPanel(this);

		initTable(controller, parts);
		JScrollPane scrollPane = new JScrollPane(table);

		JPanel buttonPanel = createButtonPanel(controller);

		setLayout(new BorderLayout());
		add(scrollPane, BorderLayout.CENTER);
		add(buttonPanel, BorderLayout.SOUTH);
	}

	private void initTable(final AposApplicationController controller,
			final List<RdiPart> parts) {
		tableModel = new PartTableModel(parts);
		table = new JTable(tableModel);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.getSelectionModel().addListSelectionListener(
				new ListSelectionListener() {

					@Override
					public void valueChanged(ListSelectionEvent e) {
						listSelectionPerformed(controller);
					}
				});
	}

	private JPanel createButtonPanel(final AposApplicationController controller) {
		partLabel = new JLabel("Total parts");
		partCountField = new JTextField("0", 4);
		partCountField.setEditable(false);
		countPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		countPanel.add(partLabel);
		countPanel.add(partCountField);

		orderButton = new JButton(CommonString.ORDER);
		orderButton.setEnabled(false);
		orderButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				orderPartPerformed(controller);
			}
		});

		deleteButton = new JButton(CommonString.DELETE);
		deleteButton.setEnabled(false);
		deleteButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				deletePartPerformed(controller);
			}
		});

		JPanel rightPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		rightPanel.add(countPanel);
		rightPanel.add(orderButton);

		JPanel leftPanel = new JPanel();
		leftPanel.add(deleteButton);

		JPanel buttonPanel = new JPanel(new BorderLayout());
		buttonPanel.add(leftPanel, BorderLayout.WEST);
		buttonPanel.add(rightPanel, BorderLayout.EAST);

		setLayout(new BorderLayout());
		add(buttonPanel, BorderLayout.SOUTH);

		return buttonPanel;
	}

	private void deletePartPerformed(AposApplicationController controller) {
		// TODO Auto-generated method stub
		RdiPart selectedPart = getSelectedPart();
		controller.deletePart(selectedPart);
	}

	public void update(List<RdiPart> parts) {
		partList = parts;
		tableModel.setParts(parts);
		partCountField.setText("" + parts.size());
	}

	private void orderPartPerformed(final AposApplicationController controller) {
		RdiPart selectedPart = getSelectedPart();
		controller.orderPart(selectedPart);
	}

	private void listSelectionPerformed(AposApplicationController controller) {
		boolean enable = (getSelectedPart() != null);
		orderButton.setEnabled(enable);
		deleteButton.setEnabled(enable);
		controller.partSelectedPerformed(getSelectedPart());
	}

	private RdiPart getSelectedPart() {
		int viewRowIndex = table.getSelectedRow();
		if (viewRowIndex < 0) {
			return null;
		}

		int modelIndex = table.convertRowIndexToModel(viewRowIndex);
		RdiPart selectedPart = tableModel.getVAlueAt(modelIndex);
		return selectedPart;
	}

	public void reset() {
		// TODO Auto-generated method stub
		table.setModel(new PartTableModel(new ArrayList<RdiPart>()));

	}

}
